﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DietPlanning.Models;

namespace DietPlanning.Controllers
{ 
    public class DietController : Controller
    {
        private DailyFoodEntities db = new DailyFoodEntities();

        //
        // GET: /Diet/

        public ViewResult Index()
        {
            var diets = db.Diets.Include("User");
            return View(diets.ToList());
        }

        //
        // GET: /Diet/Details/5

        public ViewResult Details(int id)
        {
            Diet diet = db.Diets.Single(d => d.DietID == id);
            return View(diet);
        }

        //
        // GET: /Diet/Create

        public ActionResult Create()
        {
            ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName");
            return View();
        } 

        //
        // POST: /Diet/Create

        [HttpPost]
        public ActionResult Create(Diet diet)
        {
            if (ModelState.IsValid)
            {
                db.Diets.AddObject(diet);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", diet.UserID);
            return View(diet);
        }
        
        //
        // GET: /Diet/Edit/5
 
        public ActionResult Edit(int id)
        {
            Diet diet = db.Diets.Single(d => d.DietID == id);
            ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", diet.UserID);
            return View(diet);
        }

        //
        // POST: /Diet/Edit/5

        [HttpPost]
        public ActionResult Edit(Diet diet)
        {
            if (ModelState.IsValid)
            {
                db.Diets.Attach(diet);
                db.ObjectStateManager.ChangeObjectState(diet, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", diet.UserID);
            return View(diet);
        }

        //
        // GET: /Diet/Delete/5
 
        public ActionResult Delete(int id)
        {
            Diet diet = db.Diets.Single(d => d.DietID == id);
            return View(diet);
        }

        //
        // POST: /Diet/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Diet diet = db.Diets.Single(d => d.DietID == id);
            db.Diets.DeleteObject(diet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult DietPage()
        {
            var diets = db.Diets.Include("User");
            return View(diets.ToList());
        }

        public ActionResult DietDetail(int id, int day)
        {
            AccountController.UserDiet.DietDetail = db.DietDetails.Where(o => o.DietID == id).ToList();
            var diets = db.Diets.Where(o => o.DietID == id).ToList();
            AccountController.UserDiet.Diet = diets;
            AccountController.UserDiet.ViewDays = day;
            return View(AccountController.UserDiet);
            //return RedirectToAction("Index");

        }

        public ActionResult ViewFood(int dishId)
        {
           
            return View(db.Dishes.Where(o => o.DishID == dishId));
        }
    }
}